﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using System.IO;
using BLL;
using Public;

namespace QLNhanKhau
{
    public partial class Report : Window
    {
        ReportViewer viewer = new ReportViewer();

        //public Report(DateTime BD, DateTime KT, bool bprint = false)
        //{
        //    InitializeComponent();

        //    viewer.LocalReport.ReportPath = "../../Report.rdlc";
        //    List<SoHoKhauPublic> names = new List<SoHoKhauPublic>();

        //    if (bprint)
        //        viewer.RenderingComplete += new RenderingCompleteEventHandler(viewer_RenderingComplete);
            
        //    Person[] nms = { new Person("JOHN"), new Person("JAC"), new Person("HOMER") };
        //    names.AddRange(nms);
        //    names = SoHoKhauBLL.BaoCaoSHK(BD,KT);
        //    for (int i = 0; i < 60; i++)
        //        names.AddRange(nms);
        //    viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
        //}
        public Report(string filename,bool bprint = false)
        {
            viewer.LocalReport.ReportPath = "../../"+filename+".rdlc";
            if (bprint)
                viewer.RenderingComplete += new RenderingCompleteEventHandler(viewer_RenderingComplete);

            //Person[] nms = { new Person("JOHN"), new Person("JAC"), new Person("HOMER") };
            //names.AddRange(nms);
            if (filename == "NVQS")
            {
                
                List<NhanKhauPublic> names = new List<NhanKhauPublic>();
                names = NhanKhauBLL.NVQS();
                viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
            }
            if (filename == "TrinhDo")
            {
                
                List<TrinhDoPublic> names = new List<TrinhDoPublic>();
                names = TrinhDoBLL.TrinhDoReport();
                viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
            }
            if (filename == "BaoCaoDanSo")
            {

                List<SoHoKhauPublic> names = new List<SoHoKhauPublic>();
                names = SoHoKhauBLL.BaoCaoDanSo();
                viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
            }
            // for (int i = 0; i < 60; i++)
            //    names.AddRange(nms);
            //viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
        }

        void OnRenderingCompletePrintOnly(object sender, System.ComponentModel.AsyncCompletedEventArgs args)
        {
            object objviewer = viewer;
            object[] prms = { sender, args };
            PrintReportViewer.ExecuteFunction(objviewer, prms, "OnRenderingCompletePrintOnly");
        }
        bool streamed = false;
        Stream CreateStreamEMFPrintOnly(string name, string extension, Encoding encoding, string mimeType, bool useChunking, Microsoft.ReportingServices.Interfaces.StreamOper operation)
        {
            if (streamed)
                return null;
            streamed = true;
            object objviewer = viewer;
            object[] prms = { name, extension, encoding, mimeType, useChunking, operation };
            Stream str = (Stream)PrintReportViewer.ExecuteFunction(objviewer, prms, "CreateStreamEMFPrintOnly");
            return str;
        }
        public Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream CreateAndRegisterStream
        {
            get
            {
                return new Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream(CreateStreamEMFPrintOnly);
            }
        }
        public System.ComponentModel.AsyncCompletedEventHandler AsyncCompletedEventHandler
        {
            get
            {
                return new System.ComponentModel.AsyncCompletedEventHandler(this.OnRenderingCompletePrintOnly);
            }
        }
        public void Print()
        {

            viewer.RefreshReport();

        }
        public void PrintByPrinter(string Printername)
        {
            PrintReportViewer.PrintByPriner(this, viewer, Printername);

        }
        void viewer_RenderingComplete(object sender, RenderingCompleteEventArgs e)
        {
            PrintReportViewer.PrintwithDialog(viewer);
        }
    }
}
